/**
 * 
 */
package com.noisyair.android.alvin.apps.sms;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * This Class binds the custom TextMessage objects to the set
 * of Views that are laid out in the corresponding XML layout'
 * file.  
 * 
 * NOTE: only constructor 4 has been tested to work properly, so
 * hopefully that is the only one that gets called. 
 */
public class MessageAdapter extends ArrayAdapter <TextMessage> {

	int mResource;
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		LinearLayout messageView;
		TextMessage message = getItem(position);
		
		String body = message.getMMessage();
		String date = message.getDate();
		String name = message.getMName();
		
		if(convertView == null){
			messageView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;	
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
			vi.inflate(mResource, messageView, true);
		}
		else{
			messageView = (LinearLayout)convertView;
		}
		
		TextView bodyView = (TextView)messageView.findViewById(R.id.messagebody);
		TextView nameView = (TextView)messageView.findViewById(R.id.messageperson);
		TextView dateView = (TextView)messageView.findViewById(R.id.messagedate);

		bodyView.setText(body);
		nameView.setText(name);
		dateView.setText(date);
		
		// Change color of view based on if it was read or not:
		if(!message.wasRead()){
			bodyView.setBackgroundColor(0x55BBBBBB);
			nameView.setBackgroundColor(0x55BBBBBB);
			dateView.setBackgroundColor(0x55BBBBBB);		
		}	
		return messageView;		
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 */
	public MessageAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		Log.i("Alvin", " MessageAdapter: constructor 1 was called");
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 */
	public MessageAdapter(Context context, int resource, int textViewResourceId) {
		super(context, resource, textViewResourceId);
		Log.i("Alvin", " MessageAdapter: constructor 2 was called");
		mResource = resource;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public MessageAdapter(Context context, int textViewResourceId,
			TextMessage[] objects) {
		super(context, textViewResourceId, objects);
		Log.i("Alvin", " MessageAdapter: constructor 3 was called");
		mResource = textViewResourceId;
}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public MessageAdapter(Context context, int textViewResourceId,
			List<TextMessage> objects) {
		super(context, textViewResourceId, objects);
		mResource = textViewResourceId;
		Log.i("Alvin", " MessageAdapter: constructor 4 was called");
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public MessageAdapter(Context context, int resource,
			int textViewResourceId, TextMessage[] objects) {
		super(context, resource, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		Log.i("Alvin", " MessageAdapter: constructor 5 was called");

		mResource = resource;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public MessageAdapter(Context context, int resource,
			int textViewResourceId, List<TextMessage> objects) {
		super(context, resource, textViewResourceId, objects);
		Log.i("Alvin", " MessageAdapter: constructor 6 was called");
		mResource = resource;
	}
}
